using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace SideShow
{
    public abstract class SerializableObject
    {
        public void Serialize(BinaryWriter writer)
        {
            SerializeObject(writer);
        }

        public void Deserialize(BinaryReader reader)
        {
            DeserializeObject(reader);
        }

        protected internal virtual void SerializeObject(BinaryWriter writer)
        {
        }

        protected internal virtual void DeserializeObject(BinaryReader reader)
        {
        }

        public static string GetTypeName(object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            return GetTypeName(obj.GetType());
        }

        public static string GetTypeName(Type type)
        {
            return type.FullName + ", " + type.Module.Assembly.GetName().Name;
        }

        public static Type FindType(string assemblyTypeName)
        {
            Type ret = Type.GetType(assemblyTypeName);
#if !PocketPC
            if (ret == null)
            {
                string[] parts = assemblyTypeName.Split(',');
                string typeName = parts[0].Trim();
                string asmName = parts[1].Trim();
                List<Assembly> list = new List<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
                Assembly asm = list.Find(delegate(Assembly a)
                {
                    return a.GetName().Name == asmName;
                });
                if (asm != null)
                {
                    ret = asm.GetType(typeName);
                }
            }
#endif
            return ret;
        }
    }
}
